@extends('admin.tableLayout')
@section('title','文章列表')
@section('barName','文章列表')
@section('quote')
    <button type="button" class="layui-btn layui-btn-small" id="getAll" onclick="javascript:location.href='{{route('admin.posts-add')}}'"><i class="fa fa-plus" aria-hidden="true"></i> 添加文章</button>
    <hr/>
    <form class="layui-form" >
        <label>所属栏目：</label>
        <div class="layui-inline">
            <select name="category_id"  id="category_id" lay-search >
                <option value="">全部</option>
                @foreach($categories as $k=>$v)
                    @if($v['pid']==0)
                        <optgroup label="{{$v['cat_name']}}">
                            @foreach($v['son'] as $vv)
                                <option value="{{$vv['id']}}">{{$vv['cat_name']}}</option>
                                @if(isset($vv['son'])&&$vv['son'])
                                    @foreach($vv['son'] as $vvv)
                                        <option value="{{$vvv['id']}}" @if(old('category_id')) {{$vvv['id']==old('category_id')?'selected':''}} @else @if(isset($model->category_id) && $vvv['id']==$model->category_id) selected @endif @endif> ┗{{$vvv['cat_name']}}</option>
                                    @endforeach
                                @endif
                            @endforeach
                        </optgroup>
                    @endif
                @endforeach
            </select>

        </div>
        <label>县区：</label>
        <div class="layui-inline">
            <select name="area"  id="area" lay-search lay-filter="select">
                <option value="">全部</option>
                @foreach(\App\Area::all()->toArray() as $k=>$v)
                    <option value="{{$v['id']}}">{{$v['name']}}</option>
                @endforeach
            </select>

        </div>
        <label>状态：</label>
        <div class="layui-inline">
            <select name="status"  id="status" lay-search lay-filter="select">
                <option value="">全部</option>
                @foreach($status as $k=>$v)
                    <option value="{{$k}}">{{$v}}</option>
                @endforeach
            </select>

        </div>
        <label>推荐：</label>
        <div class="layui-inline">
            <select name="is_recommend"  id="is_recommend" lay-search lay-filter="select1">
                <option value="">全部</option>
                @foreach($recommend as $k=>$v)
                    <option value="{{$k}}">{{$v}}</option>
                @endforeach
            </select>
        </div>
        <div id="select" class="layui-inline"></div>

        <div class="layui-inline">
            <input type="text" class="layui-input" width="200px" id="datetime" name="searchTime" value="" placeholder="日期范围搜索">
        </div>
        <div class="layui-inline">
            <input type="text" name="searchKey" placeholder="文章名称" autocomplete="off" value="" class="layui-input">
        </div>
        <button lay-filter="search" class="layui-btn" id="search"><i class="fa fa-search" aria-hidden="true"></i> 查询</button>
    </form>
@endsection

@section('toolbar')
    @can('posts')
        <a class="layui-btn layui-btn-mini" lay-event="chose">选择</a>
    @endcan
@endsection
@section('tableRender')

    <script>
        layui.use(['table','jquery','form'], function(){
            var table = layui.table;
            var $=layui.jquery;
            var form=layui.form;
            var category_id=$("#category_id").val();
            var _renderLoad = layer.msg('正在查找文章，请稍候',{icon: 16,time:false,shade:0.8});
            ;
            if(category_id!=''){
                $.get('/admin/writing/get_theme',{category_id:category_id},function (data) {
                    if(data!=''){
                        $("#select").html(data);
                        form.render('select');
                    }else{
                        $("#select").html('');
                    }
                })
            }
            var render=table.render({
                elem:"#table",
                page:true,
                id:'idTest',
                url:'{{route('admin.wechat.send.newsListAjax')}}',
                where:{
                    searchKey:$("input[name='searchKey']").val(),
                    searchTime:$("input[name='searchTime']").val(),
                    category_id:$("#category_id").val(),
                    area_id:$("#area").val(),
                    status:$("#status").val(),
                    is_recommend:$('#is_recommend').val()
                },
                height:400,
                cols:  [[ //标题栏
                    {field: 'id', title: 'ID', width: 80,fixed:true}
                    ,{field: 'post_title', title: '标题', width: 200},
                    {field: 'category_id', title: '栏目', width: 200},
                    {fixed: 'right', width:250,title:'操作', align:'center', toolbar: '#toolbar'}
                    //这里的toolbar值是模板元素的选择器
                ]],
                response: {//返回格式自定义
                    statusName: 'code' //数据状态的字段名称，默认：code
                    ,statusCode: 0 //成功的状态码，默认：0
                    ,msgName: 'msg' //状态信息的字段名称，默认：msg
                    ,countName: 'total' //数据总数的字段名称，默认：count
                    ,dataName: 'data' //数据列表的字段名称，默认：data
                },
                done:function () {
                    layer.close(_renderLoad);
                }
            });
            $('#search').on('click',function () {
                var data = {
                    searchKey:$("input[name='searchKey']").val(),
                    searchTime:$("input[name='searchTime']").val(),
                    category_id:$("#category_id").val(),
                    area_id:$("#area").val(),
                    status:$("#status").val(),
                    is_recommend:$('#is_recommend').val()
                };
                _renderLoad = layer.msg('正在查找文章，请稍候',{icon: 16,time:false,shade:0.8});
                render.reload(
                    {
                        where:data
                    }
                );
                return false;
            })
            table.on('checkbox(table)', function(obj){
                //console.log(obj)
            });

            table.on('tool(table)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值
                var tr = obj.tr; //获得当前行 tr 的DOM对象
                if(layEvent === 'delete'){ //删除
                    layer.confirm('真的删除行么', function(index){
                        layer.close(index);
                        //向服务端发送删除指令
                        $.get('{{route('admin.posts',['act'=>'delete'])}}',{id:data.id},function(e){
                            if(e.info=='success'){
                                layer.msg('操作成功',{icon:1});
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            }
                        });
                    });
                } else if(layEvent === 'chose'){ //编辑
                    parent.news = data;
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭                       //do something
                    //同步更新缓存对应的值
                }
            });
        });
    </script>
@endsection

